precision mediump float;

uniform sampler2D texture0;
uniform int width;
uniform vec2 resolution;

varying vec2 fragTexCoord;

vec4 average_row(sampler2D tex, vec2 uv, int width, vec2 resolution) {
    float r = 0.0, g = 0.0, b = 0.0;
    for (int i = 0; i < 640; ++i) { // 注意：需要动态传入 width
        vec2 sample_uv = vec2(float(i) / resolution.x, uv.y);
        vec4 color = texture2D(tex, sample_uv);
        r += color.r;
        g += color.g;
        b += color.b;
    }
    r /= float(width);
    g /= float(width);
    b /= float(width);
    return vec4(r, g, b, 1.0);
}

void main() {
    vec2 uv = fragTexCoord;
    gl_FragColor = average_row(texture0, uv, width, resolution);
}